package com.yx.gsyd.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.gsyd.entity.SysJydz;
import com.yx.gsyd.entity.excel.SysJydzExcel;
import com.yx.gsyd.mapper.SysJydzMapper;
import com.yx.gsyd.qpage.QPage;
import com.yx.gsyd.service.ISysJydzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.gsyd.util.ResultView;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-29
 */
@Service
public class SysJydzServiceImpl extends ServiceImpl<SysJydzMapper, SysJydz> implements ISysJydzService {


    /**
     * 列表
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public ResultView getPageList(QPage qPage, SysJydz obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<SysJydz> queryWrapper=new QueryWrapper<>();
        if(obj.getJyName()!=null&&!"".equals(obj.getJyName())){
            queryWrapper.like("jy_name",obj.getJyName());
        }
        iPage = this.baseMapper.selectPage(iPage,queryWrapper);
        return ResultView.ok(iPage);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Transactional
    @Override
    public ResultView add(SysJydz obj) {
        QueryWrapper<SysJydz> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("jy_name",obj.getJyName());
        SysJydz sysJydz=this.getOne(queryWrapper);
        if(sysJydz!=null){
            return ResultView.error("经营地址名称已存在");
        }
        boolean isOk=this.save(obj);
        return ResultView.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResultView del(String id) {
        this.removeById(id);
        return ResultView.ok();
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Transactional
    @Override
    public ResultView up(SysJydz obj) {
        boolean isOk=this.updateById(obj);
        return ResultView.ok();
    }

    @Override
    public ResultView importExcel(List<SysJydzExcel> ulist) {
        for (SysJydzExcel sysJydzExcel : ulist) {
            QueryWrapper<SysJydz> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("jy_name",sysJydzExcel.getJyName());
            SysJydz sysJydz=this.getOne(queryWrapper);
            if(sysJydz==null){
                SysJydz sysJydz1=new SysJydz();
                sysJydz1.setJyName(sysJydzExcel.getJyName());
                this.save(sysJydz1);
            }
        }
        return ResultView.ok();
    }


}
